<?php
!defined('QAPP') AND exit('Forbidden');
$connectOK = false;
$site_db = array();

if (session_get("site_db")) {
    $site_db = session_get("site_db");
} else {
    $site_db = array('host' => '127.0.0.1', 'port' => '3306', 'dbname' => 'qapp', 'user' => 'root', 'password' => '', 'pre' => '');
}

if (getPost("host")) {
    $site_db['host'] = getPost("host");
}
if (getPost("port")) {
    $site_db['port'] = getPost("port");
}

if (getPost("user")) {
    $site_db['user'] = getPost("user");
}

if (getPost("pw")) {
    $site_db['password'] = getPost("pw");
}
if (getPost("dbname")) {
    $site_db['dbname'] = getPost("dbname");
}

if (getPost("pre")) {
    $site_db['pre'] = getPost("pre");
}

//dumpForInstall($site_db);

function checkMysql()
{
    global $connectOK, $site_db;
    if (getPost("host") && getPost('user') && getPost('pw')) {
        if (!file_exists("../../core/data/mysql.sql")) {
            echo "<script>alert('core/data/mysql.sql不存在，此文件用于创建最基本的QAPP数据库结构')</script>";
        }

//        $site_db = array('database' => "mysqlpdo", 'host' => getPost("host"), 'dbname' => '', 'user' => getPost('user'), 'password' => getPost('pw'));
        $dbtest = new QAppDB();
        if ($dbtest->mysqlTestConnect($site_db)) {
            $connectOK = true;
            echo "<script>alert('MySQL数据库[成功]连接')</script>";
        } else {
            echo "<script>alert('MySQL数据库[未成功连接]，请检查参数')</script>";
        }
    } else {
        echo "<script>alert('MySQL连接参数不全，请填写[主机地址]、[用户名]、[密码]')</script>";
    }
}

if (getGet('errmsg')) {
    echo "<script>alert('" . getGet('errmsg') . "')</script>";
}

if (getGet("submit") == 1) {
    $dbtype = getPost("dbtype");
    if ($dbtype == "sqlite") {
        if (!file_exists("../../core/data/sys.db")) {
            echo "<script>alert('core/data/sys.db不存在，此文件用于创建最基本的QAPP数据库结构')</script>";
        } else {
            $site_db = array('database' => "sqlitepdo");
            session_set("step", 50);
            session_set('site_db', $site_db);
            header("location:?step=50");
            exit();
        }
    } elseif ($dbtype == "mysql") {
        checkMysql();
        if ($connectOK) {
            $site_db = array('database' => "mysqlpdo", 'host' => getPost("host"), 'port' => getPost("port"), 'dbname' => getPost("dbname"), 'user' => getPost('user'), 'password' => getPost('pw'), 'pre' => getPost('pre', ''));
            session_set("step", 50);
            session_set('site_db', $site_db);
            header("location:?step=50");
            exit();
        }
    } else {
        echo "<script>alert('不支持的数据库类型')</script>";
    }
} elseif (getGet("test") == 1) {
    checkMysql();
}

$have_sqlite = ext_exist("pdo_sqlite");
$have_mysql = ext_exist("pdo_mysql");
if ($have_sqlite && $have_mysql) {
    echo "<p class='alert alert-warning' style='color:#ff0054;text-align:center;font-size: 20px'>请选择【SQLite】或【MySQL】作为QAPP的数据库</p>";
}
if ($have_sqlite) {
    ?>
    <div class="panel panel-default" style="margin-bottom: 0" id="webserver">
        <div class="panel-heading"><b style="color: #ff0054">SQLite</b></div>
        <div class="panel-body">
            <div style="text-align: center">
                <p>QAPP将自动配置SQLite数据库，数据库将存放在<code>application/data</code>目录下</p>
                <form action="?step=40&submit=1" method="post" onsubmit="return confirm('确定选择SQLite作为QAPP的数据库？')">
                    <input type="hidden" name="dbtype" value="sqlite">
                    <button type="submit" class="btn btn-primary">选择SQLite数据库</button>
                </form>
            </div>
        </div>
    </div>

    <?php
    if ($have_mysql && $have_sqlite) {
        echo "<div class='row' style='text-align: center;padding: 20px;font-size: 20px;font-weight: bold;color: #ff0054'>或</div>";
    }
}

if ($have_mysql) {
    ?>
    <div class="panel panel-default" id="webserver">
        <div class="panel-heading"><b style="color: #ff0054">MySQL</b></div>
        <div class="panel-body">
            <form id="mysqlForm" method="post" action="">
                <input type="hidden" name="dbtype" value="mysql">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="form-group">
                            <label>MySQL主机地址</label>
                            <input id="host" value="<?php echo array_value($site_db, 'host') ?>" name="host" type="text"
                                   class="form-control" placeholder="建议使用IP，如：127.0.0.1，用localhost会存在DNS解析延迟">
                        </div>
                        <div class="form-group">
                            <label>端口</label>
                            <input id="port" value="<?php echo array_value($site_db, 'port') ?>" name="port" type="text"
                                   class="form-control" placeholder="默认：3306">
                        </div>
                        <div class="form-group">
                            <label>数据库名称</label>
                            <input id="dbname" value="<?php echo array_value($site_db, 'dbname') ?>" name="dbname"
                                   type="text"
                                   class="form-control" placeholder="如：qapp">
                            <p class="help-block">如果不存在，安装程序将尝试新建</p>
                        </div>
                    </div>
                    <div class="col-lg-6">
                        <div class="form-group">
                            <label>数据表前缀[可选]</label>
                            <input id="pre" value="<?php echo array_value($site_db, 'pre') ?>" name="pre" type="text"
                                   class="form-control" placeholder="默认为空">
                            <p class="help-block">不推荐使用表前缀，应该尽可能考虑单独的数据库安装单独的应用</p>
                            <p class="help-block">如果数据库中已经存在了数据表，为了防止冲突和混乱，建议加前缀。<br>如果是单独的数据库，则可以留空</p>
                        </div>

                        <div class="form-group">
                            <label>数据库用户名</label>
                            <input id="user" value="<?php echo array_value($site_db, 'user') ?>" name="user" type="text"
                                   class="form-control" placeholder="如：root">
                        </div>
                        <div class="form-group">
                            <label>数据库用户密码</label>
                            <input id="pw" value="<?php echo array_value($site_db, 'password') ?>" name="pw"
                                   type="password"
                                   class="form-control">
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div style="text-align: center">
                            <p class="alert alert-info">建议测试连接成功后再选择</p>
                            <button type="button" class="btn btn-default" onclick="mysqltest()">测试连接</button>
                            <button type="button" class="btn btn-primary" onclick="mysqlselect()">选择MySQL数据库</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script>
        function mysqltest() {
            document.getElementById("mysqlForm").action = "?step=40&test=1&dbtype=mysql"
            checkform()

        }

        function mysqlselect() {
            document.getElementById("mysqlForm").action = "?step=40&submit=1&dbtype=mysql"
            if (confirm("确定选择MySQL作为QAPP的数据库？")) {
                checkform()
            }

        }

        function getInputVal(id) {
            var ele = document.getElementById(id);
            if (ele) {
                return ele.value
            } else {
                return null
            }
        }

        function checkform() {
            if (!getInputVal("host")) {
                alert("请填写数据库主机地址")
                return false;
            }
            if (!getInputVal("port")) {
                alert("请填写数据库端口")
                return false;
            }
            if (!getInputVal("dbname")) {
                alert("请填写数据库名")
                return false;
            }
            if (!getInputVal("user")) {
                alert("请填写数据库用户名")
                return false;
            }
            if (!getInputVal("pw")) {
                alert("请填写数据库密码")
                return false;
            }

            document.getElementById("mysqlForm").submit()
        }
    </script>
    <?php
}
?>

<?php if (session_get("dbtype")): ?>

    <div id="next" style="margin: 30px;text-align: center;">
        <p>您选择了：<?php echo session_get("dbtype"); ?>数据库</p>
        <p>
            <a class="btn btn-primary" href="?step=50&checkok=1">开始安装</a>
        </p>
    </div>

<?php endif; ?>